package com.yyaccp.jdbc.util;

import java.sql.*;

public class DataSourceUtils {
    public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName(ConfigManager.getInstance().getValue("driverClassName"));
            conn = DriverManager.getConnection(ConfigManager.getInstance().getValue("url"),
                    ConfigManager.getInstance().getValue("username"),
                    ConfigManager.getInstance().getValue("password"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return conn;
    }

    public static void close(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(PreparedStatement pstmt) {
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void closeAll(ResultSet rs, PreparedStatement pstmt, Connection conn) {
        close(rs);
        close(pstmt);
        close(conn);
    }

}
